package com.mini.mq;

import com.mini.constant.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangxiaobin
 * @title: QueueConfig
 * @description: TODO
 * @date 2020/2/2111:19
 */
@Configuration
public class QueueConfig {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Bean
    public TopicExchange teacherSayExchange() {
        TopicExchange contractTopicExchange = new TopicExchange(MqConstants.TEACHER_SAY_EXCHANGE_NAME);
        rabbitAdmin.declareExchange(contractTopicExchange);
        return contractTopicExchange;
    }

    @Bean
    public Queue teacherSayQueue() {
        Queue queue = new Queue(MqConstants.TEACHER_SAY_QUEUE_NAME, true);
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding teacherSayBind() {
        Binding binding = BindingBuilder.bind(teacherSayQueue()).to(teacherSayExchange()).with(MqConstants.TEACHER_SAY_ROUTING_NAME);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }


    @Bean
    public Queue queueMessage() {
        return new Queue(MqConstants.QUEUE_CHANCE_FOLLOW);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(MqConstants.EXCHANGE_DATA);
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with(MqConstants.ROUTE_KEY_SITE);
    }

    @Bean
    public TopicExchange dataTopicExchang() {
        TopicExchange topicExchange = new TopicExchange(MqConstants.EXCHANGE_DATA);
        rabbitAdmin.declareExchange(topicExchange);
        return topicExchange;
    }

    @Bean
    public Queue dataCommonQueue() {
        Queue queue = new Queue(MqConstants.QUEUE_COMMON, true);
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding dataCommonMsgBind() {
        Binding binding = BindingBuilder.bind(dataCommonQueue()).to(dataTopicExchang()).with(MqConstants.ROUTE_KEY_COMMON);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }


}
